//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

import { invoke } from '@tauri-apps/api/tauri';

export type SERVER_TYPE = number;

export const SERVER_TYPE_SSH: SERVER_TYPE = 0;
export const SERVER_TYPE_MYSQL: SERVER_TYPE = 1;
export const SERVER_TYPE_POSTGRES: SERVER_TYPE = 2;
export const SERVER_TYPE_MONGO: SERVER_TYPE = 3;
export const SERVER_TYPE_REDIS: SERVER_TYPE = 4;
export const SERVER_TYPE_GRPC: SERVER_TYPE = 5;

export type BasicServerInfo = {
    server_name: string;
    server_type: SERVER_TYPE;
    addr_list: string[];
};

export type ServerInfo = {
    server_id: string;
    basic_info: BasicServerInfo;
    create_time: number;
    update_time: number;
};

export type AddServerRequest = {
    session_id: string;
    project_id: string;
    basic_info: BasicServerInfo;
};

export type AddServerResponse = {
    code: number;
    err_msg: string;
    server_id: string;
};

export type UpdateServerRequest = {
    session_id: string;
    project_id: string;
    server_id: string;
    basic_info: BasicServerInfo;
};

export type UpdateServerResponse = {
    code: number;
    err_msg: string;
};

export type ListServerRequest = {
    session_id: string;
    project_id: string;
};

export type ListServerResponse = {
    code: number;
    err_msg: string;
    server_info_list: ServerInfo[];
};

export type GetServerRequest = {
    session_id: string;
    project_id: string;
    server_id: string;
};

export type GetServerResponse = {
    code: number;
    err_msg: string;
    server_info: ServerInfo;
};

export type RemoveServerRequest = {
    session_id: string;
    project_id: string;
    server_id: string;
};

export type RemoveServerResponse = {
    code: number;
    err_msg: string;
};

//增加服务器
export async function add_server(request: AddServerRequest): Promise<AddServerResponse> {
    const cmd = 'plugin:project_server_api|add_server';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<AddServerResponse>(cmd, {
        request,
    });
}

//更新服务器
export async function update_server(request: UpdateServerRequest): Promise<UpdateServerResponse> {
    const cmd = 'plugin:project_server_api|update_server';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<UpdateServerResponse>(cmd, {
        request,
    });
}

//列出服务器
export async function list_server(request: ListServerRequest): Promise<ListServerResponse> {
    const cmd = 'plugin:project_server_api|list_server';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<ListServerResponse>(cmd, {
        request,
    });
}

//获取单个服务器
export async function get_server(request: GetServerRequest): Promise<GetServerResponse> {
    const cmd = 'plugin:project_server_api|get_server';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<GetServerResponse>(cmd, {
        request,
    });
}

//删除服务器
export async function remove_server(request: RemoveServerRequest): Promise<RemoveServerResponse> {
    const cmd = 'plugin:project_server_api|remove_server';
    console.log(`%c${cmd}`, 'color:#0f0;', request);
    return invoke<RemoveServerResponse>(cmd, {
        request,
    });
}